from gaev.actions.model import details
from gaev import actions
from gaev import HumanizeContext
from gaev import utils

from data import models

class ProblemSolutionsSection(details.Section):
	template='details/problem_solutions.html'
	humanize_context=HumanizeContext(use_html=True)
	max_object_count=10
	
	def get_provers_query(self):
		return models.ProverInstance.all().order('-added')

	def get_params(self):
		from data import analyzer
		problem=self.entity
		provers_query=self.get_provers_query()
		provers=provers_query
		results=analyzer.get_problem_prover_table([problem], provers)[problem.short]
		provers_data_all=analyzer.get_provers_data(provers, self.humanize_context)
		provers_data=utils.filter(lambda x:x['short'] in results, provers_data_all)
		
		params={
			'provers': provers_data,
			'results': results,
			}
		return params

class ProverResultsSection(details.Section):
	template='details/prover_results.html'
	humanize_context=HumanizeContext(use_html=True)
	max_object_count=10
	
	def get_problems(self):
		prover=self.entity
		resQuery=models.Result.all().filter('prover = ', prover)
		results=resQuery.fetch(30)
		problem_set=set()
		for res in results:
			problem_set.add(res.problem)
			
		problems=list(problem_set)
		problems.sort(lambda p1, p2: cmp(p1.short, p2.short))
		
		return problems

		
	def get_params(self):
		from data import analyzer
		prover=self.entity
		problems=self.get_problems()
		results=analyzer.get_prover_problem_table(problems=problems, provers=[prover])[prover.short]
		problems_data_all=analyzer.get_problems_data(problems, self.humanize_context)
		problems_data=utils.filter(lambda x:x['short'] in results, problems_data_all)
		
		params={
			'problems': problems_data,
			'results': results,
			}
		return params

class ProverResultCartAnalyzerAction(actions.TemplateAction):
	template='base_cart_analyzer.html'
	humanize_context=HumanizeContext(use_html=True)
	
	def get_items(self):
		from gaev.actions import cart
		return cart.get_cart_items()
	
	def get_domain(self):
		from gaev.utils import UniqueList
		items=self.get_items()
		
		problems, provers = UniqueList(), UniqueList()
		
		for itm in items:
			if isinstance(itm, models.Problem):
				problems.append(itm)
			elif isinstance(itm, models.ProblemGroup):
				keys=itm.problems
				problems.extend(models.Problem.get(keys))
			elif isinstance(itm, models.Division):
				for problem in itm.problem_set.order('short'):
					problems.append(problem)
			elif isinstance(itm, models.AxiomFamily):
				for ax_set in itm.axiomset_set.order('short'):
					ax_problems=models.Problem.all().filter('axioms = ', ax_set.key()).order('short')
					problems.extend(ax_problems)
			elif isinstance(itm, models.AxiomSet):
				ax_problems=models.Problem.all().filter('axioms = ', itm.key()).order('short')
				problems.extend(ax_problems)
			elif isinstance(itm, models.ProverInstance):
				provers.append(itm)
			elif isinstance(itm, models.ProverVersion):
				provers.extend(itm.proverinstance_set)
			elif isinstance(itm, models.Prover):
				for ver in itm.proverversion_set:
					provers.extend(ver.proverinstance_set)
		
		return problems, provers
		
	def perform_template(self):
		from data import analyzer
		problems, provers = self.get_domain()
		
		results=analyzer.get_problem_prover_table(problems, provers)
		
		problems_data_all=analyzer.get_problems_data(problems, self.humanize_context)
		problems_data=utils.filter(lambda x:results[x['short']]['result_count']>0, problems_data_all)
		
		provers_data=analyzer.get_provers_data(provers, self.humanize_context)
		
		params={
			'problems': problems_data,
			'provers': provers_data,
			'results': results,
			}
		return params